package com.dosh;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

/**
 * This activity shows the informations of the current User and gives the possibility to change them.
 */
public class Admin_User extends Activity implements OnClickListener{
	
	DBAdapter db = new DBAdapter(this);
	TextView name, firstname, username, register, pwa, pw, pwr;
	TextView name_text, firstname_text, username_text, register_text, pwa_text, pw_text, pwr_text;
	Button change_pw, change_inf, commit1, commit2;
	
	/**
	 * This method loads all Buttons and Textfields.
	 */
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.admin_user);
        
      //Connects the variable with its field
        name = (TextView) findViewById(R.id.admin_user_lastname_field);
        firstname = (TextView) findViewById(R.id.admin_user_firstname_field);
        username = (TextView) findViewById(R.id.admin_user_username_field);
        register = (TextView) findViewById(R.id.admin_user_date_field);
        pwa = (TextView) findViewById(R.id.admin_user_pwa_field);
        pw = (TextView) findViewById(R.id.admin_user_pw_field);
        pwr = (TextView) findViewById(R.id.admin_user_pwr_field);
        
        name_text = (TextView) findViewById(R.id.admin_user_lastname_text);
        firstname_text = (TextView) findViewById(R.id.admin_user_firstname_text);
        username_text = (TextView) findViewById(R.id.admin_user_username_text);
        register_text = (TextView) findViewById(R.id.admin_user_date_text);
        pwa_text = (TextView) findViewById(R.id.admin_user_pwa_text);
        pw_text = (TextView) findViewById(R.id.admin_user_pw_text);
        pwr_text = (TextView) findViewById(R.id.admin_user_pwr_text);
        
        change_pw  = (Button) findViewById(R.id.admin_user_chg_pw);
    	change_pw.setOnClickListener(this);
    	change_inf  = (Button) findViewById(R.id.admin_user_chg_inf);
     	change_inf.setOnClickListener(this);
     	commit1  = (Button) findViewById(R.id.admin_user_commit_inf);
     	commit1.setOnClickListener(this);
     	commit2  = (Button) findViewById(R.id.admin_user_commit_pw);
     	commit2.setOnClickListener(this);
     	
     	name.setText(MainActivity.currentUser.surname);
     	firstname.setText(MainActivity.currentUser.firstname);
     	username.setText(MainActivity.currentUser.username);
     	register.setText(MainActivity.currentUser.registerDate);
     	
     	name.setEnabled(false);
     	firstname.setEnabled(false);
     	username.setEnabled(false);
     	register.setEnabled(false);
     	
     	pwa.setVisibility(View.GONE);
        pw.setVisibility(View.GONE);
        pwr.setVisibility(View.GONE);
        pwa_text.setVisibility(View.GONE);
        pw_text.setVisibility(View.GONE);
        pwr_text.setVisibility(View.GONE);
        commit1.setVisibility(View.GONE);
        commit2.setVisibility(View.GONE);
        
    }
	/**
	 * Changes the informations of the User (password excluded)
	 * @param lastname New last name
	 * @param firstname New first name
	 * @param username New username
	 */
	public void changeInformations(String lastname, String firstname, String username){
		MainActivity.currentUser.update("Surname", lastname, db);
		MainActivity.currentUser.update("Firstname", firstname, db);
		MainActivity.currentUser.update("Username", username, db);
		Toast.makeText(Admin_User.this, "Userdates changed", Toast.LENGTH_LONG).show();
	}
	/**
	 * Changes the password of the User
	 * @param pw New password
	 * @param pwr New password reputation
	 * @param pwa Old password
	 */
	public void changePassword(String pw, String pwr, String pwa){
		if(!pw.equals(pwr)){
			Toast.makeText(Admin_User.this, "False Passwords!", Toast.LENGTH_LONG).show();
		}
		else if(db.md5(pwa).equals(MainActivity.currentUser.password)){
			Toast.makeText(Admin_User.this, "Password changed", Toast.LENGTH_LONG).show();
			MainActivity.currentUser.update("Password", db.md5(pw), db);
		}else{
			Toast.makeText(Admin_User.this, "Password not correct", Toast.LENGTH_LONG).show();
		}
		
	}
	/**
	 * This method handles the OnClickListener. 
	 * Change Informations or the password of the current User.
	 */
	public void onClick(View v) {
		if(v==change_inf){
			//Makes the needed fields visible
			name.setEnabled(true);
	     	firstname.setEnabled(true);
	     	username.setEnabled(true);
	     	commit1.setVisibility(View.VISIBLE);
	     	change_inf.setVisibility(View.GONE);
		}
		else if(v==change_pw){
			//Makes the needed fields visible
			pwa.setVisibility(View.VISIBLE);
	        pw.setVisibility(View.VISIBLE);
	        pwr.setVisibility(View.VISIBLE);
	        pwa_text.setVisibility(View.VISIBLE);
	        pw_text.setVisibility(View.VISIBLE);
	        pwr_text.setVisibility(View.VISIBLE);
	        commit2.setVisibility(View.VISIBLE);
	        change_pw.setVisibility(View.GONE);
		}
		else if(v==commit1){
			//Changes the informations of the User an sets the fields disabled
			this.changeInformations(name.getText().toString(), firstname.getText().toString(), username.getText().toString());
			name.setEnabled(false);
	     	firstname.setEnabled(false);
	     	username.setEnabled(false);
	     	commit1.setVisibility(View.GONE);
	     	change_inf.setVisibility(View.VISIBLE);
		}
		else if(v==commit2){
			//Changes the password of the User an sets the fields invisible
			this.changePassword(pw.getText().toString(), pwr.getText().toString(), pwa.getText().toString());
			pwa.setVisibility(View.GONE);
	        pw.setVisibility(View.GONE);
	        pwr.setVisibility(View.GONE);
	        pwa_text.setVisibility(View.GONE);
	        pw_text.setVisibility(View.GONE);
	        pwr_text.setVisibility(View.GONE);
	        commit2.setVisibility(View.GONE);
	        change_pw.setVisibility(View.VISIBLE);
	        
	        pwa.setText("");
	        pw.setText("");
	        pwr.setText("");
		}
		
	}
	/**
	 * This method is called, when the Activity gets resumed
	 */
    @Override
    protected void onResume() {
    	super.onResume();
    	Bundle tempBundle = new Bundle();
    	onCreate(tempBundle);
    }
    
    
}
